package com.example.singletones;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import android.util.Log;

import com.example.singletones.userSettingsData.UserConfData;

public class userSettings {

	private static userSettings instance = null;
	public static UserConfData userData;
	private final File Dir;

	public userSettings() {
		Dir = android.os.Environment.getExternalStorageDirectory();
		userData = new UserConfData();
	}

	public static userSettings getInstance() {
		if (instance == null) {
			instance = new userSettings();
			if (!instance.loadModel()) {
				instance.loadModel();
			}
			;
		}
		return instance;
	}

	public boolean SaveModel() {

		userData.setUserName();
		userData.setPassword();
		try {
			File yourFile = new File(Dir, "/userSettings.bcf");
			if (!yourFile.exists()) {
				yourFile.createNewFile();
			}
			FileOutputStream fileOut = new FileOutputStream(
					Dir.getAbsolutePath() + "/userSettings.bcf");
			ObjectOutputStream out = new ObjectOutputStream(fileOut);
			out.writeObject(userData);
			out.close();
			fileOut.close();
		} catch (IOException i) {
			Log.d("onStart", i.getMessage());
			return false;
		}
		return true;
	}

	public boolean loadModel() {
		try {
			FileInputStream fileIn = new FileInputStream(Dir.getAbsolutePath()
					+ "/userSettings.bcf");
			ObjectInputStream in = new ObjectInputStream(fileIn);
			userData = (UserConfData) in.readObject();
			in.close();
			fileIn.close();
		} catch (IOException i) {
			SaveModel();
			return false;
		} catch (ClassNotFoundException c) {
			SaveModel();
			return false;
		}
		return true;
	}
}
